package userManagement;

import java.io.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;

import db.DatabaseConnector;

public class DelUser extends HttpServlet {
    public void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
	DatabaseConnector DBConnect = new DatabaseConnector(DatabaseConnector.getDatabaseType());

        try {
            out.println("<html><head>");
            out.println("<title>Delete User</title></head><body bgcolor=FFFFFF>");
            String username = request.getParameter("username");

            DBConnect.deleteSQL("USER_ROLES","username",username);
            DBConnect.deleteSQL("USER_INFO","username",username);
            DBConnect.deleteSQL("USERS","username",username);

            String SQL = "UPDATE chatUsers SET removed = 0 WHERE username = ?";
            DBConnect.freeformSQL(SQL,new String[]{username});

            out.println("<p>User "+request.getParameter("username")+" was successfully removed from the Database.</p>");
            out.println("<form method=\"post\" action=\"ListUsers\">");
            out.println("<input type=\"submit\" value=\"Back to User List\" /></form>");

            out.println("</body></html>");
            out.close();
        }
        finally
        {
            DBConnect.CleanUp();
        }
    }

	public void doGet(HttpServletRequest request, HttpServletResponse response)
	    throws ServletException, IOException {
			doPost(request, response);
	}
}